package com.example.springboot.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.springboot.common.Result;
import com.example.springboot.common.ResultCode;
import com.example.springboot.domain.po.News;
import com.example.springboot.exception.CustomException;

import com.example.springboot.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.controller
 * @Project：springboot
 * @name：NewsController
 * @Date：2025/2/4 21:20
 * @Filename：NewsController
 * @for:
 */
@Api(tags = "新闻管理接口")
@RestController
@CrossOrigin
@RequestMapping("/news")
public class NewsController {
       @Resource
       private NewsService newsService;

       /**
        * 查询新闻接口
        * @return 返回新闻列表
        */
       @ApiOperation("查询新闻接口")
       @GetMapping("/alldata")
       public Result getData(){
              List<News> newsList = newsService.getAll();
              if (newsList.size() < 1){
                     throw new CustomException(ResultCode.DATA_USER_LESS);
              }
              return Result.success(newsList);
       }

       @ApiOperation("分页查询新闻接口")
       @PostMapping("/page")
       public Result page(@RequestBody News search,
                          @RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "5") Integer pageSize){
              return Result.success(newsService.findPage(search, pageNum, pageSize));
       }

       @ApiOperation("新增新闻接口")
       @PostMapping
       public Result newsRegister(@RequestBody News news){
              return Result.success(newsService.add(news));
       }

       @ApiOperation("删除新闻接口")
       @DeleteMapping("/{id}")
       public Result delete(@PathVariable Integer id){
              newsService.delete(id);
              return Result.success();
       }

       @ApiOperation("修改新闻接口")
       @PutMapping
       public Result update(@RequestBody News news){
              newsService.newsEdit(news);
              return Result.success();
       }


       @ApiOperation("查询最新发布的8条数据")
       @GetMapping("/newdata")
       public Result getNewData(){
              return Result.success(newsService.GetNewData());
       }

       @ApiOperation("根据id查询")
       @GetMapping("/{id}")
       public Result getById(@PathVariable Integer id){

              return Result.success(newsService.findById(id));
       }





}